package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.entity.Blog;
import org.top10.entity.CollectEntity;
import org.top10.entity.Finance;
import org.top10.entity.Forum;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class EastmoneyTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_EastMoney;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_GDK;// 对应网站ID
	
	public EastmoneyTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		EastmoneyBlogParser bp=new EastmoneyBlogParser();
		bp.setEncoding(TOP10Global.Encoding_UTF8);
		PARSER.add(bp);
		PARSER.add(new EastmoneyGubaParser());
	}
	public static void main(String arg[]) throws Exception{
		EastmoneyBlogParser bp=new EastmoneyBlogParser();
		bp.setEncoding(TOP10Global.Encoding_UTF8);
		bp.dealText(bp.getEncoding());
		System.out.println("--------------------------------");
		new EastmoneyFinanceParser().dealText(ENCoding);
	}
	
}
abstract class EastmoneyParser  extends TOPParserImpl{
	private String baseUrl;
	public EastmoneyParser(String c, int t, String u,String baseUrl) {
		super(c, t, u);
		this.baseUrl=baseUrl;
	}
	public abstract List<CollectEntity> parse(ParseSource doc) throws Exception ;

	public  List<CollectEntity> parse1(ParseSource doc,String sign,String tagName,int increase) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node = getNodeByText(body, tagName,sign).getParentNode().getParentNode();
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int rank=1;
		for (int i = 0; i < as.size(); i=i+increase) {
			Node tr=as.get(i);
			CollectEntity v =build(tr,rank++);
			System.out.println(v);
			list.add(v);
			if(i>=9*increase)break;
		}
		return list;
	}
	public  List<CollectEntity> parse1(ParseSource doc,String sign,String tagName) throws Exception {
		return parse1(doc,sign,tagName,2);
	}
	public  void build1(Node node, int rank,CollectEntity v) throws Exception {
		v.setRank(rank);
		v.setName(node.getTextContent());
		String url=getAttrValueByTag(node,"href");
		v.setLinkUrl(baseUrl+url);
	}
	
}
class EastmoneyGubaParser extends EastmoneyParser{
	private static final String URL="http://guba.eastmoney.com/";
	private static final String BaseUrl=URL;
	private static final String SIGN="48小时股吧热门文章排行";
	public EastmoneyGubaParser() {
		super("股吧排行", TOP10Global.TYPE_Forum, URL,BaseUrl);
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return parse1(doc,SIGN,"div");
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		 Forum f=new Forum();
		 build1(node,rank,f);
		 return f;
	}
	
}
class EastmoneyBlogParser extends EastmoneyParser {
	
	private static final String URL="http://blog.eastmoney.com/24hour.html";
	private static final String SIGN="24小时博文点击榜";
	public EastmoneyBlogParser() {
		super("博客排行",TOP10Global.TYPE_Blog, URL,"");
	}
	public  List<CollectEntity> parse(ParseSource doc) throws Exception {
		return  parse1(doc,SIGN,"b");
	}

	public  CollectEntity build(Node node, int rank) throws Exception {
		Blog b=new Blog();
		build1(node,rank,b);
		return b;
	}
}
class EastmoneyFinanceParser  extends EastmoneyParser {
	
	private static final String URL="http://finance.eastmoney.com/pinglun.html";
	private static final String SIGN="48小时点击排行";
	public EastmoneyFinanceParser() {
		super("财经排行",TOP10Global.TYPE_Finance, URL,"");
	}
	public  List<CollectEntity> parse(ParseSource doc) throws Exception {
		return  parse1(doc,SIGN,"div",1);
	}

	public  CollectEntity build(Node node, int rank) throws Exception {
		Finance f=new Finance();
		build1(node,rank,f);
		return f;
	}
}